package performance;

import java.io.File;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class RunComplexRuntimeTest {

	public static void main(String[] args) throws InterruptedException {
		final StringBuffer builder = new StringBuffer();
		ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
		service.execute(new Runnable(){

			@Override
			public void run() {
				RunComplexRuntimeTest.runAdditionTest(builder);
			}
		});
		service.shutdown();
		service.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
		
		System.out.println(builder.toString());
		
	}
	
	private static void runAdditionTest(StringBuffer builder){
		ComplexRuntimeTest test = new ComplexAdditionRuntimeTest();
		
		DataCollector<Long> collector = new RuntimeDataCollector();
		long startTime = System.currentTimeMillis();
		test.runTest(collector);
		long endTime = System.currentTimeMillis();
		long totalTimeRun = endTime-startTime;
		Collection<Long> results = collector.getResults();
		
		long averageRunTime=0;
		int numTestsRun = results.size();
		for(Long result: results){
			averageRunTime +=result;
		}
		
		averageRunTime = averageRunTime/numTestsRun;
		builder.append("AdditionRuntimeTest\n");
        builder.append("     total tests run: ").append(numTestsRun).append("\n");
        builder.append("     total time taken: ").append(totalTimeRun).append(" ms\n");
        builder.append("     average run time: ").append(averageRunTime).append(" ms\n");
	}
	

}
